package com.auto.survey.base

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.auto.survey.activity.bluetooth.BluetoothService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class BaseModle : ViewModel() {

    val bluetoothMsg =  MutableLiveData<String>()
    val bluetoothState =  MutableLiveData<BluetoothService.ConnectionState>()

    //收集蓝牙设备发送的信息
    fun receiverBluetoothMsg(message:String) {
        viewModelScope.launch {
            val result = withContext(Dispatchers.IO) {
                message
            }
            bluetoothMsg.value = result
        }
    }

    //更新蓝牙的连接状态
    fun updateBluetoothStat(state: BluetoothService.ConnectionState) {
        viewModelScope.launch {
            val result = withContext(Dispatchers.IO) {
                state
            }
            bluetoothState.value = result
        }
    }
}